package com.firexxx.vp.domain.stb;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.util.StringUtils;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import java.util.Objects;

/**
 * @description: TODO
 * @throws:
 * @author: zhuangzhang2
 * @date: 2019/12/18 17:28
 */
@Embeddable
@Getter
@NoArgsConstructor
@ToString
public class StbKey {

    @NotBlank
    @Column(name = "channel_id")
    private String channelId;

    @Column(name = "stb_id")
    private String stbId;

    @Column
    private String mac;

    @Transient
    private String uniqueKey;

    public StbKey(String channelId, String stbId, String mac) {
        this.channelId = channelId;
        this.stbId = stbId;
        this.mac = mac;

        if (!StringUtils.isEmpty(stbId)) {
            uniqueKey = stbId;
        } else if (!StringUtils.isEmpty(mac)) {
            uniqueKey = mac;
        } else {
            throw new IllegalStateException("stbid and mac both null");
        }
    }

    public String getUniqueKey() {
        if (!StringUtils.isEmpty(uniqueKey)) {
            return uniqueKey;
        }

        if (!StringUtils.isEmpty(stbId)) {
            uniqueKey = stbId;
        } else if (!StringUtils.isEmpty(mac)) {
            uniqueKey = mac;
        }

        return uniqueKey;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        StbKey stbInfo = (StbKey) o;
        return Objects.equals(uniqueKey, stbInfo.uniqueKey);
    }

    @Override
    public int hashCode() {
        return Objects.hash(uniqueKey);
    }
}